/**
 * related to caseCenter.ui
 * 
 * @Author : 2037630@163.com
 * @Timestamp : 2017-04-19
 */
var notify = sm("do_Notification");
var Controller = require("core/controller");
var constant = require('config/constant');
var log = require("util/log");
var event = require("config/event");
var server = require('config/server');
var net = require("util/net");
var navigate = require("util/navigate")
var router = require('config/router')
var user = require("module/userModule");
var filter = require('util/filter')
var global=sm('do_Global')
var external=sm("do_External")
var paging = require('util/paging')
var deleteBox=ui('deleteIcon')
var root=ui("$")
var common=require('util/common')
var caseBg=ui("caseBg")
var staFont=null;
var staBg=null;
var boxBorder=null;
Controller({
    mapping : {
        "caseCenter" : "caseCenterData",  
        "hintBox.text":"params.custName",
        "caseBg.bgImage":"caseBg"
    },
    ui:{
        caseCenter:'caseCenter',
        caseBg:'caseBg',
        hintBox:'hintBox'
    },
    data : {
        caseCenterData : [],
        paging:paging.create(),
        params:{
            custName:'',
              
        },
    	caseBg:''
        
    },
    event : {
        '$page' :{
            "telDiallog" : function(id) {
                external.openDial(id.id);
            },
            "mapPage":function(id){
                
            }
        },
        'searchBox' : {
            'touch' : function() {
                navigate.redirectTo({
                    view : router.user.caseSearch,
                    back:function(data){
                        //按照客户名称重新进行搜索
                        this.data.caseCenterData = []
                        this.data.params.custName = data
                        this.isDispear()
                        this.refresh('$')
                        this.getCaseList() 
                    },
                    scope:this
                })
            }
        },
        'caseCenter' : {
            'touch' : function(index) {
                //获取点击list的数据并发送到下一级页面
                var data = this.data.caseCenterData[index]
                var custId = data.custId
                navigate.redirectTo({ 
                    view : router.home.caseDetail,
                    data : {
                        //设置要发送的数据
                        current: data,
                        cust:custId
                    }
                })
            },
            /**
             * 列表下拉刷新
             */
            'pull':function(data){
                if(data.state == 2){
                    this.data.caseCenterData = []
                    this.data.paging.reset()
                    this.getCaseList()
                }
            },
            /**
             * 列表上拉更新
             */
            'push':function(data){
                if(data.state == 2){
                    if(this.data.paging.next()){
                        this.getCaseList()
                    }else{
                        this.ui.caseCenter.rebound()
                    }
                }
            }
        },
        'mess_iv':{
            'touch':function(){
                //跳转消息列表
                navigate.redirectTo({
                    view : router.user.userMessage
                })
            }
        },
//        清空
        'deleteIcon':{
            'touch':function(){
                this.data.params.custName=''
                this.ui.hintBox.text=""
                this.isDispear()
                this.data.caseCenterData = []
                this.data.paging.reset()
                this.getCaseList()
            }
        }
        
    },
    methods: {
        /**
         * 获取案件列表
         */
        getCaseList:function(){
            return net.send({
                server : server.organization.search,
                page:this.data.paging,
                data:{
                    "custName":this.data.params.custName||'',
                    "cupoRectype":70
                },
                success : function(data) {
                    //数据转码以及数据格式转化
                    log.info("sss")
                    log.info(data.content)
                    var list = data.content.map(function(x) {
//                    	判断还款状态颜色
                    		if(x.cupoStatus==57){
                    			staFont="#FF9813FF";
                    			staBg="#FFF5E9FF"
                    			boxBorder="#FF9813FF"+",1,[0,0,0,0]"
                    		}
                    		else{
                    			staFont="#0191DAFF";
                    			staBg="#E3F2FDFF"
                    			boxBorder="0191DAFF"+",1,[0,0,0,0]"
                    		}
//                    		数据转码等操作
                            x.cupoStatus= filter.typeCodeConvert(x.cupoStatus)
                            x.cupoNow =Date.parse(new Date())
                            x.cupoEndday=Date.parse(x.cupoEndday)
                            var temp=x.cupoEndday-x.cupoNow
	                    	temp=(temp>0)?temp:0
                            x.cupoCountdown =Math.floor(temp/(1000*60*60*24))
                            x.id=x.cupoPhone
                            x.cupoAmt=x.cupoAmt.toFixed(2)
                            x.cupoPrid=filter.typeCodeConvert(x.cupoPrid)
                            x.staFont=staFont,
                            x.staBg=staBg,
                            x.boxBorder=boxBorder,
                            x.mapData={
                              custName:x.custName,
                              mapInfo:x.mapInfo,
                              detailAddress:x.address
                            }
                            return x 
                    })
                    log.info(list)
                    this.data.caseCenterData.push.apply(this.data.caseCenterData,list);
                    this.refresh("caseCenter");
//                    this.goBlank()
                },
                error : function(data) {
//                	this.goBlank()
                    log.info(data)
                },
                callback:function(){
                    this.ui.caseCenter.rebound()
                },
                scope : this
            }) 
        },
//      判断删除图标的消失
        isDispear:function(){
            if(!this.data.params.custName){
                deleteBox.visible=false
            }
            else{
                deleteBox.visible=true
            }
        },
        goBlank:function(){
//          无数据时载入空白页
            if(this.data.caseCenterData.length==0){
            	log.info("00000")
            	this.ui.caseBg.bgImage="source://image/app/blank.png"
            	this.ui.caseCenter.visible=false
            }
            else{
            	log.info("66666")
            	this.ui.caseBg.bgImage=""
            	this.ui.caseCenter.visible=true
            }
            
        }
    },
    loaded : function() {
        this.isDispear()
        // 初始化案件列表
        this.loadReady([this.getCaseList()])
    }
})